package com.spm.librarianmanagersystem.controller;

import com.spm.librarianmanagersystem.config.aop.WebLog;
import com.spm.librarianmanagersystem.service.BookInfoService;
import com.spm.librarianmanagersystem.utils.FormatUtil;
import com.spm.librarianmanagersystem.utils.JsonResult;
import com.spm.librarianmanagersystem.utils.UserContextHolder;
import com.spm.librarianmanagersystem.vo.BookDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Map;

/**
 *  @Class: BookInfoController
 *  @Author: Wu Yue
 *  @Time: 15:36 2022/4/4
 *  @Description:
 */
@RestController
@RequestMapping("/api/book")
public class BookInfoController {

    @Autowired
    BookInfoService bookInfoService;
    @Autowired
    FormatUtil formatUtil;
    @WebLog(description = "搜索图书")
    @GetMapping("/search")
    public JsonResult<ArrayList<Map<String, String>>> searchBook(
                    @RequestParam("book_name") String bookName,
                    @RequestParam("author") String author,
                    @RequestParam("publisher") String publisher,
                    @RequestParam("ISBN") String ISBN,
                    @RequestParam("category") String category,
                    @RequestParam("pub_year") String pubYear,
                    @RequestParam("pub_month") String pubMonth) {

        BookDetails bookDetails = new BookDetails();
        bookDetails.setBookName(bookName);
        bookDetails.setAuthor(author);
        bookDetails.setPublisher(publisher);
        bookDetails.setISBN(ISBN);
        bookDetails.setCategory(category);

        String publishYear = pubYear;
        String publishMonth = pubMonth;
        Integer y = 0;
        Integer m = 0;
        if(!(publishMonth.equals("")) && !(publishYear.equals(""))) {
            y = Integer.valueOf(publishYear);
            m = Integer.valueOf(publishMonth);
            bookDetails.setPublishedDate(new Date(y - 1900, m - 1, 1));
            System.out.println(bookDetails);
        } else {
            bookDetails.setPublishedDate(null);
        }
        Integer formatCode = formatUtil.dateFormat(y, m, 0, 0);
        if (!formatCode.equals(0)) {
            return new JsonResult<>(null, formatCode, formatUtil.getErrorMap().get(formatCode));
        }
        ArrayList<Map<String, String>> books = null;
        try {
            books = (ArrayList<Map<String, String>>) bookInfoService.searchBookByMessage(bookDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult<>(books, -1, "unknow error!");
        }
        return new JsonResult<>(books, 0, "Search suessfully!");
    }

    @WebLog(description = "图书的具体信息")
    @GetMapping("/detail")
    public JsonResult<Map<String, String>> detailBook(@RequestParam("ISBN") String ISBN) {

        BookDetails bookDetails = new BookDetails();
        bookDetails.setISBN(ISBN);
        Map<String, String> book = null;
        try {
            book = bookInfoService.detailBookByISBN(bookDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult<>(null, -1, "unknow error!");
        }

        return new JsonResult<>(book, 0, "Search suessfully!");
    }

    @WebLog(description = "图书在馆信息")
    @GetMapping("/locations")
    public JsonResult<ArrayList<Map<String, String>>> locationBook(@RequestParam("ISBN") String ISBN) {
        ArrayList<Map<String, String>> locations = bookInfoService.searchBookLocationByISBN(ISBN);
        return new JsonResult<>(locations, 0, "Search suessfully!");
    }
}
